package top.hcode.hoj.service.oj.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.mapper.LeavelsMapper;
import top.hcode.hoj.mapper.UserInfoMapper;
import top.hcode.hoj.pojo.entity.user.Leavels;
import top.hcode.hoj.pojo.entity.user.UserInfo;
import top.hcode.hoj.service.oj.LeavelsService;

import java.util.List;

@Service
public class LeavelsServiceImpl implements LeavelsService {
    @Autowired
    private LeavelsMapper leavelsMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;



    @Override
    public CommonResult<List<Leavels>> getdeplists(String oj) {
       return   CommonResult.successResponse(leavelsMapper.getdeplists(oj));
    }

    @Override
    public CommonResult<List<Leavels>> getlists(String oj) {
        return CommonResult.successResponse(leavelsMapper.selectList(null));
    }

    @Override
    public CommonResult<Void> adddleavels(Leavels leavels) {
        String uuid20 = String.format("%d%019d", new java.util.Random().nextInt(9) + 1, new java.util.Random().nextLong()).substring(0, 20);
        leavels.setId(uuid20);
        QueryWrapper<Leavels> wrapper = new QueryWrapper<>();
        wrapper.eq("name", leavels.getName()); // WHERE majorid = #{majorid}
        Leavels leavels1 = leavelsMapper.selectOne(wrapper);
        if (leavels1!=null) {
            return CommonResult.errorResponse("年级不可重复");
        }
        if (leavels.getName() == null || leavels.getName().equals("")) {
            return CommonResult.errorResponse("不可以为空");
        }
        int i = leavelsMapper.insert(leavels);
        if (i > 0) {
            return CommonResult.successResponse("新增完成");
        }else {
            return CommonResult.errorResponse("新增失败");
        }
    }

    @Override
    public CommonResult<Void> updatedleavels(Leavels leavels) {
        QueryWrapper<Leavels> wrapper = new QueryWrapper<>();
        wrapper.eq("name", leavels.getName()); // WHERE majorid = #{majorid}
        Leavels leavels1 = leavelsMapper.selectOne(wrapper);
        if (leavels1!=null & leavels.getName().equals(leavels.getName())) {
            return CommonResult.errorResponse("年级不可重复");
        }
        int i = leavelsMapper.updateById(leavels);
        if (i > 0) {
            return CommonResult.successResponse("更新完成");
        }else {
            return CommonResult.errorResponse("更新失败");
        }
    }

    @Override
    public CommonResult<Void> deleteleavels(Leavels leavels) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("leavelsid", leavels.getId()); // WHERE majorid = #{majorid}
        List<UserInfo> userList = userInfoMapper.selectList(wrapper);
        if (userList.size() > 0) {
            return CommonResult.errorResponse("已经绑定学生不可删除");
        }else {
            leavelsMapper.deleteById(leavels.getId());
            return CommonResult.successResponse();
        }
    }


}
